package vn.demand.dragon.model
{
	import mx.collections.XMLListCollection;
	import mx.rpc.IResponder;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.model.business.LoadCustomersDelegate;
	import vn.demand.dragon.model.business.RemoveCustomerDelegate;
	import vn.demand.dragon.model.business.SaveCompanyDelegate;
	import vn.demand.dragon.model.vo.Company;
	
	public class CompanyProxy extends Proxy implements IProxy, IResponder
	{
		public static const NAME:String = "CompanyProxy";
		
		public function CompanyProxy(proxyId:String = NAME)
		{
			super(proxyId);
		}
		
		public function loadCustomers(o:Object = null):void
		{
			// create a worker who will go get some data
			// pass it a reference to this proxy so the delegate knows where to return the data
			var delegate : LoadCustomersDelegate = new LoadCustomersDelegate(resultSearchCustomer, faultSearchCustomer);
			// make the delegate do some work
			delegate.loadCustomersService(o);
		}
		
		// this is called when the delegate receives a result from the service
		public function resultSearchCustomer( rpcEvent : Object ) : void
		{
			// populate the employee list in the proxy with the results from the service call
			var list = new XMLListCollection(rpcEvent.result.companies.company);
			sendNotification( CommonCommand.LOAD_CUSTOMERS_SUCCESSFUL, list, this.proxyName);
		}
		
		// this is called when the delegate receives a fault from the service
		public function faultSearchCustomer( rpcEvent : Object ) : void 
		{
			sendNotification(CommonCommand.LOAD_CUSTOMERS_FAILED, new XMLListCollection(), this.proxyName);
		}

		public function saveCompany(companyVO:Company):void {
			var delegate:SaveCompanyDelegate = new SaveCompanyDelegate(this);
			delegate.saveCompany(companyVO);
		}

		// this is called when the delegate receives a result from the service
		public function result( rpcEvent : Object ) : void
		{
			var success:Boolean = rpcEvent.result.success == 'true'; 
			if (success) {
				var companyVO:Company = rpcEvent.token.companyVO as Company;
				companyVO.company_id = int(rpcEvent.result.company_id);
				sendNotification(CommonCommand.CUSTOMERS_UPDATED_SUCESSFUL, companyVO);
			} else {
				errorStatus = rpcEvent.result.errors;
				sendNotification(CommonCommand.CUSTOMERS_UPDATE_FAILED, errorStatus);				
			}
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void 
		{
			errorStatus = "Something wrong when save quotation lines!";
			//sendNotification(FFIAApplicationFacade.TASK_LOG_UPDATE_FAILED, errorStatus, projectVO.project_id);
		}
		
		public function remove(params:Object):void {
			var delegate:RemoveCustomerDelegate = new RemoveCustomerDelegate(
				function(rpcEvent : Object) {
					var customerVO:Company = rpcEvent.token.customerVO as Company;
					var success:Boolean = rpcEvent.result.success == 'true'; 
					if (success) {
						sendNotification(CommonCommand.DELETE_CUSTOMER_SUCCESSFUL, customerVO);
					} else {
						sendNotification(CommonCommand.DELETE_CUSTOMER_FAILED, String(rpcEvent.result.errors));
					}
				},
				function(rpcEvent : Object) {
					sendNotification(CommonCommand.DELETE_CUSTOMER_FAILED, 'Some thing wrong...');
				});
			delegate.deleteCustomer(params);
		}

		[Bindable] public var errorStatus:String = null;
	}
}